<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>导入数据</title>
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <link rel="stylesheet" href="../css/orderset.css">
        <!-- 引入组件库 -->
        <script src="../js/vue.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/axios-0.18.0.js"></script>
    </head>
    <body class="hold-transition">
    <div id="app">
        <div class="content-header">
            <h1>奶牛管理<small>数据导入</small></h1>
            <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>奶牛管理</el-breadcrumb-item>
                <el-breadcrumb-item>数据导入</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="app-container">
            <div class="box">
                <div class="box ordersetting">
                    <el-card class="box-card">
                        <div class="boxMain">
                            <el-button style="margin-bottom: 20px;margin-right: 20px" type="primary" @click="downloadTemplate()">模板下载</el-button>
                            <el-upload action="/cow/datasetting/upload"
                                       name="excelFile"
                                       :show-file-list="false"
                                       :on-success="handleSuccess"
                                       :before-upload="beforeUpload">
                                <el-button type="primary">上传文件</el-button>
                            </el-upload>
                            <el-button style="float: right" type="primary" icon="el-icon-circle-plus-outline" @click="dialogFormVisible=true">单条添加</el-button>
                        </div>
                        <div>
                            操作说明：请点击"模板下载"按钮获取模板文件，在模板文件中录入预约设置数据后点击"上传文件"按钮上传模板文件完成数据的批量导入。
                        </div>
                    </el-card>
                </div>
                <!-- 新增标签弹层 -->
                <div class="add-form">
                    <el-dialog title="发布通知" :visible.sync="dialogFormVisible">
                        <el-form ref="dataForm" :model="formData" :rules="rules" label-position="right" label-width="150px">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="奶牛编号(code)" prop="code">
                                        <el-input v-model="formData.code"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="本次产量(yield)/ml" prop="code">
                                        <el-input v-model="formData.yield"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form>
                        <div slot="footer" class="dialog-footer">
                            <el-button @click="dialogCancel()">取消</el-button>
                            <el-button type="primary" @click="handleAddOne()">确定</el-button>
                        </div>
                    </el-dialog>
                </div>
            </div>

        </div>
    </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script>
        new Vue({
            el: '#app',
            data:{
                formData:{},
                dialogFormVisible:false,
                rules: {//校验规则
                    yield: [{ required: true, message: '本次产量为必填项', trigger: 'blur' }],
                    code: [{ required: true, message: '编号为必填项', trigger: 'blur' }]
                },
            },
            created: function () {//在vue初始化时调用

            },
            methods: {
				//上传之前进行文件格式校验
                beforeUpload(file){
                    const isXLS = file.type === 'application/vnd.ms-excel';
                    if(isXLS){
                        return true;
                    }
                    const isXLSX = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                    if (isXLSX) {
                        return true;
                    }
                    this.$message.error('上传文件只能是xls或者xlsx格式!');
                    return false;
                },
				//下载模板文件
                downloadTemplate(){
                    window.location.href="../templates/dataSetting_template.xlsx";
                },
				//上传成功提示
                handleSuccess(response, file) {
                    if(response.flag){
                        this.$message({
                            message: response.message,
                            type: 'success'
                        });
                        this.initData();
                    }else{
                        this.$message.error(response.message);
                    }
                },
                dialogCancel(){
                    this.dialogFormVisible=false;
                    this.formData={};
                },
                handleAddOne(){
                    this.$refs['dataForm'].validate((valid)=>{
                        if(valid){
                            axios.post("/cow/addYield",this.formData).then((res)=>{
                                if(res.data.flag){
                                    this.$message.success(res.data.message);
                                    this.dialogFormVisible=false;
                                } else {
                                    this.formData={};
                                    this.$message.error(res.data.message+"请重新录入");
                                }
                            });
                        }else {
                            this.$message.info("数据校验失败!!!");
                        }
                    });
                }
            },
        })
    </script>
</html>